package org.supreme.org.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.supreme.basic.result.JSONResult;
import org.supreme.basic.result.PageList;
import org.supreme.org.service.IEmployeeService;
import org.supreme.org.domain.Employee;
import org.supreme.org.query.EmployeeQuery;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    public IEmployeeService employeeService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Employee employee){
        if(employee.getId()!=null){
            employeeService.updateById(employee);
        }else{
            employeeService.insert(employee);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        employeeService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(employeeService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(employeeService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody EmployeeQuery query){
        //高级查询
        EntityWrapper<Employee> queryWrapper = new EntityWrapper<>();
        queryWrapper.like("username",query.getKeyword());
        Page<Employee> page = new Page<Employee>(query.getPage(),query.getRows());
        page = employeeService.selectPage(page,queryWrapper);
        return JSONResult.success(new PageList<Employee>(page.getTotal(),page.getRecords()));
    }
    /**
     * 批量删除对象
     */
    @RequestMapping(value="/ids",method=RequestMethod.POST)
    public JSONResult deleteids(@RequestBody List<Long> ids){
        employeeService.deleteBatchIds(ids);
        return JSONResult.success();
    }
    @PostMapping("/redject/{id}")
    public JSONResult redject(@PathVariable("id") Long id){
        return employeeService.rediect(id);
    }
}
